/*
 * Copyright (c) 2003
 * XDoclet Team
 * All rights reserved.
 */
package org.xdoclet.plugin.qtags.confluence;

import java.util.Collection;
import java.util.LinkedList;

import org.generama.QDoxCapableMetadataProvider;
import org.generama.VelocityTemplateEngine;
import org.generama.WriterMapper;

import org.xdoclet.plugin.qtags.AbstractQTagsPlugin;
import org.xdoclet.plugin.qtags.QTagUtils;

import com.thoughtworks.qdox.model.DocletTag;
import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.Type;

/**
 * Generates confluence documentation for qtags. This plugin is integgrated in
 * plugin compilation.
 *
 * @author Aslak Helles&oslash;y
 * @version $Revision: 379 $
 */
public class QTagConfluencePlugin extends AbstractQTagsPlugin {
    private QTagUtils qTagUtils = new QTagUtils();
    
    public QTagConfluencePlugin(VelocityTemplateEngine templateEngine, QDoxCapableMetadataProvider metadataProvider,
        WriterMapper writerMapper) {
        super(templateEngine, metadataProvider, writerMapper);
        setFilereplace("qtags.confluence");
        setMultioutput(false);
    }

    public String getAllowedValues(JavaMethod method) {
        if (method.getReturns().isA(new Type("boolean")) 
            ||  method.getReturns().isA(new Type("boolean", 1))
            ||  method.getReturns().isA(new Type("java.lang.Boolean"))
            ||  method.getReturns().isA(new Type("java.lang.Boolean", 1))) {
            return "true, false";
        } else if (method.getTagByName("qtags.allowed-value") != null) {
            StringBuffer allowedValues = new StringBuffer();
            DocletTag[] tagsByName = method.getTagsByName("qtags.allowed-value");

            for (int i = 0; i < tagsByName.length; i++) {
                if (i > 0) {
                    allowedValues.append(", ");
                }

                allowedValues.append(tagsByName[i].getValue());
            }

            return allowedValues.toString();
        } else {
            return "Any value";
        }
    }

    public String getDefaultValue(JavaMethod method) {
        DocletTag tag = method.getTagByName("qtags.parameter");

        if (tag == null) {
            return "";
        }

        return notNull(tag.getNamedParameter("default"));
    }

    public String getArray(JavaMethod method) {
        return method.getReturns().isArray() ? "yes" : "no";
    }
    
    public String getArrayToken(JavaMethod method) {
        return method.getReturns().isArray() ? qTagUtils.getTokenizer(method) : "";
    }
    
    public String getDescription(JavaMethod method) {
        return notNull(method.getComment());
    }

    public Collection getMethodsWithTagParameters(JavaClass javaClass) {
        return qTagUtils.getMethods(javaClass);
        /*
        LinkedList methods = new LinkedList();
        JavaMethod[] allMethods = javaClass.getMethods(true);

        for (int i = 0; i < allMethods.length; i++) {
            if (hasTagParameters(allMethods[i])) {
                methods.add(allMethods[i]);
            }
        }

        return methods;
        */
    }

    public boolean shouldGenerate(Object metadata) {
        return qTagUtils.shouldGenerate(metadata);
    }

    /*
    private boolean hasTagParameters(JavaMethod method) {
        String parent = method.getParentClass().getFullyQualifiedName();
        return method.isPropertyAccessor() && !(parent.equals("java.lang.Object")) &&
        !(parent.equals("com.thoughtworks.qdox.model.DocletTag"));
    }
    */

    private String notNull(String s) {
        if (s == null) {
            return "";
        }

        return s;
    }
}